<template>
  <div class="app-container">
    <el-row :gutter="20">
      <el-col :span="4">
        <el-card class="box-card">
          <div slot="header" class="clearfix">
            <span>课程分类</span>
          </div>
          <div class="head-container">
            <el-row style="display: flex;">
              <el-input
                v-model="folderName"
                placeholder="请输入文件夹名称"
                clearable
                size="small"
                prefix-icon="el-icon-search"
              />
            </el-row>
          </div>
          <div class="head-container" style="margin-top: 10px">
            <el-tree
              :data="folderList"
              :expand-on-click-node="false"
              :filter-node-method="filterNode"
              ref="tree"
              node-key="id"
              default-expand-all
              highlight-current
              class="fileList"
              @node-click="handleNodeClick"
            >
            <span class="custom-tree-node" slot-scope="{ node, data }">
              <span>{{ node.label }}</span>
            </span>
            </el-tree>
          </div>
        </el-card>
      </el-col>
      <el-col :span="16">
        <el-table v-loading="loading" :data="fileInfoList" @selection-change="handleSelectionChange" @row-click="selectRow">
          <el-table-column type="selection" width="55" align="center" />
          <el-table-column label="文件名称" align="center" prop="name" />
          <el-table-column label="文件大小" align="center" prop="size" >
            <template slot-scope="scope">
              <span>{{scope.row.size}}MB</span>
            </template>
          </el-table-column>
          <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
            <template slot-scope="scope">
              <el-button
                  size="mini"
                  type="text"
                  icon="el-icon-download"
                  v-if="scope.row.type != 'folder'"
                  @click="handleDownload(scope.row)"
              >下载</el-button>
              <el-button
                  size="mini"
                  type="text"
                  icon="el-icon-zoom-in"
                  v-if="scope.row.type != 'folder' && isView(scope.row.url)"
                  @click="handlePreview(scope.row)"
              >查看</el-button>
            </template>
          </el-table-column>
        </el-table>

      </el-col>
      <el-col :span="4">
        <el-card class="box-card">
          <div slot="header" class="clearfix">
            <span>{{hotName}}</span>
          </div>
          <el-table :data="hotFileInfoList" :show-header="false" @row-click="handlePreview"  style="cursor: pointer">
            <el-table-column  prop="name"  show-overflow-tooltip/>
          </el-table>
        </el-card>
      </el-col>
    </el-row>
    <el-dialog title="文件预览" :visible.sync="openPreview" v-if="openPreview" width="1300px" append-to-body>
      <file-preview :src="fileUrl"></file-preview>
    </el-dialog>
  </div>
</template>

<script>
import {
  queryMyFileInfo,
  getFileInfo,
  delFileInfo,
  addFileInfo,
  setFileInfoView,
  listMyHotFileInfo
} from "@/api/fileInfo/fileInfo";
import { addFolder, myTreeselectFileInfo, updateFolder } from '../../../api/fileInfo/fileInfo'
import FilePreview from "@/components/FilePreview/index"


export default {
  name: "FileInfo",
  components:{FilePreview},
  props:["fileMode"],
  data() {
    return {
      // 遮罩层
      loading: false,
      openPreview:false,
      fileUrl:"",
      video:["mp4","avi","mkv","mov","wmv","flv","webm","3gp","mpeg","ts","rmvb"],
      voice:["mp3","wav","acc","flac","alac","ogg","wma","aiff","ape","m4a"],
      pic:["jpeg","jpg","png","gif","bmp","tiff","tif","webp","svg"],
      doc:["doc","docx","pdf","ppt","pptx","xls","xlsx","txt"],
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 文件表格数据
      fileInfoList: [],
      hotFileInfoList:[],
      defaultProps: {
        children: "children",
        label: "label"
      },
      folderList:[],
      folderName:"",
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      openFolder:false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        ancestors: null,
        name: null,
        type: null,
        url: null,
        size: null
      },
      // 表单参数
      form: {},
      formFolder:{},
      // 表单校验
      rules: {
        name: [
          { required: true, message: "名称不能为空", trigger: "blur" }
        ],
      }
    };
  },
  mounted() {
    this.getFolderList();
    this.getList();
  },
  computed:{
    hotName(){
      return "学习热点";
/*      if (this.fileMode == "video") {
        return "热点视频";
      }else if (this.fileMode == "voice") {
        return "热点音频";
      }else if (this.fileMode == "pic") {
        return "热点图片";
      }else if (this.fileMode == "doc") {
        return "热点文档";
      }else if (this.fileMode == "other") {
        return "其他热点";
      }*/
    }
  },
  watch: {
    // 根据名称筛选部门树
    folderName(val) {
      this.$refs.tree.filter(val);
    },
    fileMode(){
      this.getList();
    },
  },
  methods: {
    selectRow(row){
      if (row.type == "folder"){
        this.queryParams.parentId = row.id;
        this.getList();
        this.$refs.tree.setCurrentKey(row.id);
      }
    },
    async getFolderList() {
      let res = await myTreeselectFileInfo({ type: "folder" })
      this.folderList = res.data;
    },
    // 筛选节点
    filterNode(value, data) {
      if (!value) return true;
      return data.label.indexOf(value) !== -1;
    },
    // 节点单击事件
    handleNodeClick(data) {
      this.queryParams.parentId = data.id;
      this.handleQuery();
    },
    /** 查询文件列表 */
    getList() {
      function sortItems(items) {
        return items.sort((a, b) => {
          if (a.type === 'folder' && b.type !== 'folder') {
            return -1;
          }
          if (a.type !== 'folder' && b.type === 'folder') {
            return 1;
          }
          return a.seqNo - b.seqNo;
        });
      }
      this.loading = true;
      if (this.fileMode == "video") {
        this.queryParams.types = this.video;
        this.queryParams.notTypes = []
      }else if (this.fileMode == "voice") {
        this.queryParams.types = this.voice;
        this.queryParams.notTypes = []
      }else if (this.fileMode == "pic") {
        this.queryParams.types = this.pic;
        this.queryParams.notTypes = []
      }else if (this.fileMode == "doc") {
        this.queryParams.types = this.doc;
        this.queryParams.notTypes = []
      }else if (this.fileMode == "other") {
        this.queryParams.types = [];
        this.queryParams.notTypes = [...this.video,...this.voice,...this.pic,...this.doc,"folder"];
      }
      queryMyFileInfo(this.queryParams).then(response => {
        let fileInfoList = response.data;
        this.fileInfoList = sortItems(fileInfoList)
        this.total = response.total;
        this.loading = false;
      });
      listMyHotFileInfo(this.queryParams).then(response => {
        let fileInfoList = response.data;
        this.hotFileInfoList = fileInfoList
      });
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        parentId: this.queryParams.parentId,
        ancestors: null,
        name: null,
        type: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        url: null,
        seqNo:1,
        size: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          addFileInfo(this.form).then(response => {
            this.$modal.msgSuccess("新增成功");
            this.open = false;
            this.getList();
          });
        }
      });
    },
    handleDownload(row){
      setFileInfoView({id:row.id})
      window.open(process.env.VUE_APP_BASE_API+row.url)
    },
    handlePreview(row){
      let type = row.type;
      setFileInfoView({id:row.id})
      if (type == "pdf"){
        window.open(process.env.VUE_APP_BASE_API+row.url)
      }else {
        this.openPreview = true;
        this.fileUrl = process.env.VUE_APP_BASE_API+row.url;
      }
    },
  }
};
</script>
<style lang="scss">
.custom-tree-node {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: space-between;
  font-size: 14px;
  padding-right: 8px;
  .el-button [class*=el-icon-] + span{
    margin-left: 0px;
  }
}
</style>
